package com.sctvcloud.qingchuan.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;

import com.sctvcloud.qingchuan.R;
import com.sctvcloud.qingchuan.application.Constances;
import com.sctvcloud.qingchuan.base.BaseActivity;
import com.sctvcloud.qingchuan.beans.NewsItem;
import com.sctvcloud.qingchuan.ui.adapter.holder.VideoPlayHolder;
import com.sctvcloud.qingchuan.ui.utils.GlideUtil;
import com.sctvcloud.qingchuan.ui.widget.BasePlayerView;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 短视频单屏播放适配器
 *
 * @author suming
 * @date 2020/6/8 14:35
 */
public class VideoPlayAdapter2 extends RecyclerView.Adapter<VideoPlayHolder> {
    public static final int VIEWTYPE_ADVANCE = 0;//预告
    public static final int VIEWTYPE_TITLE = 1;//标题
    public static final int VIEWTYPE_LIVE = 2;//直播的item
    public static final int VIEWTYPE_ADVANCE_LIST = 4;//直播预告列表

    private BaseActivity activity;
    private VideoPlayHolder.NewsItemClickListener mItemClickListener;
    private VideoPlayHolder.NewsItemTitleClickListener mCommentClickListener;
    private VideoPlayHolder.NewsItemDoUpClickListener mDoUpClickListener;
    private VideoPlayHolder.ShareClickListener mShareClickListener;
    private List<NewsItem> datas;
    private Context context;
    private OnMuteChangeListener mOnMuteChangeListener;
    private Handler mHandler = new Handler();

    public VideoPlayAdapter2(Context context) {
        datas = new ArrayList<>();
        this.context = context;
    }

    public void setActivity(BaseActivity activity) {
        this.activity = activity;
    }

    public void setMuteChangeListener(OnMuteChangeListener listener) {
        this.mOnMuteChangeListener = listener;
    }

    public void setNewsItemListener(VideoPlayHolder.NewsItemClickListener listener) {
        this.mItemClickListener = listener;
    }

    public void setTitleClickListener(VideoPlayHolder.NewsItemTitleClickListener listener) {
        this.mCommentClickListener = listener;
    }

    public void setDoUpClickListener(VideoPlayHolder.NewsItemDoUpClickListener listener) {
        this.mDoUpClickListener = listener;
    }

    public void setShareClickListener(VideoPlayHolder.ShareClickListener listener) {
        this.mShareClickListener = listener;
    }

    @Override
    public VideoPlayHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.video_play_item2, parent, false);
        return new VideoPlayHolder(v);
    }

    public void setData(List<NewsItem> list) {
        if (null != list) {
            datas = list;
        }
        notifyDataSetChanged();
    }

    public void addData(List<NewsItem> list) {
        int pos = getItemCount();
        if (null != list) {
            datas.addAll(list);
        }
        if (pos > 0) {
            notifyItemRangeChanged(pos, getItemCount());
        }
    }

    public void updateData(List<NewsItem> list) {
        if (null != list) {
            datas = list;
        }
    }

    @Override
    public int getItemViewType(int position) {
        return datas.get(position).getDataViewType();
    }

    @Override
    public void onBindViewHolder(final VideoPlayHolder holder, @SuppressLint("RecyclerView") final int position) {
        final NewsItem info = datas.get(position);
        String title = info.getNewsTitle();
        holder.title.setText(title);

        if (null != holder.videoPlayer && !TextUtils.isEmpty(info.getNewsMediaUrl())) {
            holder.setCanPlay(true);
            holder.noUrlImg.setVisibility(View.GONE);
            holder.videoPlayer.setVisibility(View.VISIBLE);
            holder.videoPlayer.setMuteChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (null != mOnMuteChangeListener) {
                        mOnMuteChangeListener.OnMuteChange(isChecked);
                    }
                }
            });
            holder.videoPlayer.setOnPlayerStatusListener(new BasePlayerView.OnPlayerStatusChanged() {

                @Override
                public void OnPlaying() {

                }

                @Override
                public void OnError() {

                }

                @Override
                public void OnStop() {

                }

                @Override
                public void OnEnd() {
                    mHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if (holder.videoPlayer != null) {
                                holder.playVideo();
                            }
                        }
                    }, 100);
                }
            });
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (holder.videoPlayer.getThumbImge() != null && null != activity && !activity.isDestroyed()) {
                        GlideUtil.getGlideWithLarge169Def(context, info.getNewsImage()).centerCrop().into(holder.videoPlayer.getThumbImge());
                    }
                    if(info.getNewsMediaUrl().startsWith("http")||info.getNewsMediaUrl().startsWith("https")){
                        holder.videoPlayer.setUp(info.getNewsMediaUrl(), false, "");
                    }else {
                        holder.videoPlayer.setUp(Constances.ST_BASE_URL + info.getNewsMediaUrl(), false, "");
                    }

                    holder.videoPlayer.onVideoPause();

                }
            }, 200);
        } else {
            holder.setCanPlay(false);
            holder.videoPlayer.setVisibility(View.GONE);
            holder.noUrlImg.setVisibility(View.VISIBLE);
            GlideUtil.getGlideWithLarge169DefNotFit(context, info.getNewsImage()).into(holder.noUrlImg);
        }
        holder.coverImg.setVisibility(View.VISIBLE);
        holder.coverImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mItemClickListener) {
                    mItemClickListener.OnItemClick(position);
                }
            }
        });
        if (info.isDigg()) {
            Drawable drawable = context.getResources().getDrawable(R.mipmap.dianzan_press);
            holder.doUp.setCompoundDrawablesRelativeWithIntrinsicBounds(null, drawable, null, null);
            holder.doUp.setTextColor(context.getResources().getColor(R.color.colorTxRedMain_));
        } else {
            Drawable drawable = context.getResources().getDrawable(R.mipmap.dianzan_normal);
            holder.doUp.setCompoundDrawablesRelativeWithIntrinsicBounds(null, drawable, null, null);
            holder.doUp.setTextColor(context.getResources().getColor(R.color.white));
        }
        /*********************点赞******************/
        holder.doUp.setText(MessageFormat.format("{0}", info.getCountDigg()));
        holder.doUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mDoUpClickListener) {
                    mDoUpClickListener.OnItemDoUpClick(position, info.getNewsId());
                }
            }
        });
        /*********************评论******************/
        holder.comment_num.setText(getCommentShowing(info.getCommentNum()));
        holder.comment_num.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mCommentClickListener) {
                    mCommentClickListener.OnItemTitleClick(position, info);
                }
            }
        });
        /*********************分享******************/
        holder.share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mShareClickListener) {
                    mShareClickListener.OnShareClick(position, info.getNewsSharedUrl());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }


    public interface OnMuteChangeListener {
        void OnMuteChange(boolean isMute);
    }

    private String getCommentShowing(int count) {
        String str;
        if (count > 0) {
            if (count > 99) {
                str = "99+";
            } else {
                str = String.valueOf(count);
            }
        } else {
            str = "0";
        }
        return str;
    }
}
